package cz.proks.schoolpupilbook.gae.restlet.resource;

import java.util.LinkedList;
import java.util.List;

import cz.proks.schoolpupilbook.gae.dao.CourseDAO;
import cz.proks.schoolpupilbook.gae.dao.ListDAO;
import cz.proks.schoolpupilbook.gae.dao.MarkDAO;
import cz.proks.schoolpupilbook.gae.dao.UserCourseRelationDAO;
import cz.proks.schoolpupilbook.gae.dao.UserDAO;
import cz.proks.schoolpupilbook.gae.dao.UserRelationDAO;
import cz.proks.schoolpupilbook.shared.domain.Course;
import cz.proks.schoolpupilbook.shared.domain.IsIndexableDomain;
import cz.proks.schoolpupilbook.shared.domain.Mark;
import cz.proks.schoolpupilbook.shared.domain.User;
import cz.proks.schoolpupilbook.shared.domain.User.Role;
import cz.proks.schoolpupilbook.shared.domain.UserCourseRelation;
import cz.proks.schoolpupilbook.shared.domain.UserRelation;
import cz.proks.schoolpupilbook.shared.restlet.RestMapping;
import cz.proks.schoolpupilbook.shared.util.Tester;

public class ListServerResource extends BaseServerResource implements ListResource{	
	private final ListDAO dao = new ListDAO();
	
	@Override
	public LinkedList<? extends IsIndexableDomain> list() {
		String actionMapStr = (String) getRequestAttributes().get(RestMapping.PARAMETER_LIST_ACTION);
		ListMethodMapping action = ListMethodMapping.forRestMapping(actionMapStr);
		
		if (action == null)
			return null;
		
		switch (action) {
			case COURSES:
				return (LinkedList<Course>) listCourses();	
				
			case USERS:
				return (LinkedList<User>) listUsers();		
				
			case MARKS:
				return (LinkedList<Mark>) listMarks();
				
			case USER_RELATIONS:
				return (LinkedList<UserRelation>) listUserRelations();
				
			case USER_COURSE_RELATIONS:
				return (LinkedList<UserCourseRelation>) listUserCourseRelations();
	
			case COURSES_FOR_STUDENT:
				return (LinkedList<Course>) listCoursesForStudent();
				
			case STUDENTS_FOR_COURSE:
				return (LinkedList<User>) listStudentsForCourse();
				
			case STUDENTS_FOR_PARENT:
				return (LinkedList<User>) listStudentsForParent();
				
			case MARKS_FOR_COURSE_FOR_STUDENT:
				return (LinkedList<Mark>) listMarksForCourseForStudent();		
			
			case USERS_BY_ROLE:
				return (LinkedList<User>) listUsersByRole();
				
			default:
				throw new IllegalArgumentException();
		}		
	}
	
	private List<User> listUsers() {		
		List<User> users = new UserDAO().list();			
		return users;
	}
	
	private List<Course> listCourses() {
		List<Course> courses = new CourseDAO().list();  		
		return courses;
	}
	
	private List<Mark> listMarks() {
		List<Mark> marks = new MarkDAO().list();
		return marks;
	}
	
	private List<UserRelation> listUserRelations() {
		List<UserRelation> userRelations = new UserRelationDAO().list();
		return userRelations;
	}
	
	private List<UserCourseRelation> listUserCourseRelations() {
		List<UserCourseRelation> userCourseRelations = new UserCourseRelationDAO().list();
		return userCourseRelations;
	}

	private List<User> listStudentsForCourse() {
		String courseId = getQuery().getValues(RestMapping.PARAMETER_COURSE_ID);	
		if (Tester.isEmpty(courseId))			
			return new LinkedList<User>();
		
		List<User> students = dao.listUsersForCourse(courseId);  
		return students;
	}

	private List<User> listStudentsForParent() {
		String parentId = getQuery().getValues(RestMapping.PARAMETER_USER_ID);
		
		if (Tester.isEmpty(parentId))
			return new LinkedList<User>();
		
		List<User> students = dao.listUsersForParent(parentId);  
		return students;
	}

	private List<Course> listCoursesForStudent() {
		String userId = getQuery().getValues(RestMapping.PARAMETER_USER_ID);;
		
		if (Tester.isEmpty(userId))
			return new LinkedList<Course>();
		
		List<Course> courses = dao.listCoursesForUser(userId);  		
		return courses;
	}

	private List<Mark> listMarksForCourseForStudent() {
		String studentId = getQuery().getValues(RestMapping.PARAMETER_USER_ID);
		String courseId = getQuery().getValues(RestMapping.PARAMETER_COURSE_ID);
		
		if (Tester.isEmpty(studentId) || Tester.isEmpty(courseId))
			return new LinkedList<Mark>();
		
		List<Mark> marks = dao.listMarksForCourseAndUser(studentId, courseId);
		return marks;		
	}	
	
	private List<User> listUsersByRole() {
		String roleStr = getQuery().getValues(RestMapping.PARAMETER_USER_ROLE);
		
		if (Tester.isEmpty(roleStr))
			return new LinkedList<User>();
		
		Role role = Role.valueOf(roleStr);
		List<User> users = dao.listUsersByRole(role);
		return users;
	}
}
